const http = require('http')
const fs = require('fs')
const Url = require('url')
const path = require('path')
// const querystring = require('querystring')
// let d = url.parse(`https://www.baidu.com/s?wd=canvas&rsv_spt=1&rsv_iqid=0xe4c3bbc8000107e7&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=ib&rsv_sug3=7&rsv_sug1=3&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=761&rsv_sug4=2741`)
// console.log(d)
// console.log(querystring.parse(d.query))

let server = http.createServer((req, res) => {
    const { pathname } = Url.parse(req.url)
    console.log(pathname)
    if (pathname === '/user') {
        let filePath = path.resolve(__dirname, `./public/user.html`)
        fs.createReadStream(filePath).pipe(res)
    } else if (pathname === '/orderList') {
        let d = [
            {
                id: 1, data: 'mac'
            },
            {
                id: 2, data: 'Iphone 12'
            },
        ]
        res.end(JSON.stringify(d))
    } else {
        let filePath = path.resolve(__dirname, `./public/${pathname}`)
        let exist = fs.existsSync(filePath)
        if (exist) {
            fs.createReadStream(filePath).pipe(res)
        } else {
            res.statusCode = 404
            res.end('404')
        }
    }

})

server.listen(3000, () => {
    console.log('server is runing port: 3000')
})
